package com.henry.chatkit.weight.photoview

import com.github.iielse.imageviewer.ImageViewerBuilder
import com.github.iielse.imageviewer.ImageViewerDialogFragment
import com.github.iielse.imageviewer.core.DataProvider
import com.henry.chatkit.utils.appActivity

object ImageHelper {

    var orientationH: Boolean = true
    var loadAllAtOnce: Boolean = true
    var fullScreen: Boolean = true
    var simplePlayVideo: Boolean = true

    fun show(clickedData: PhotoData) {

        val builder = ImageViewerBuilder(
            context = appActivity(),
            initKey = clickedData.id,
            dataProvider = myDataProvider(clickedData),
            imageLoader = ChatImageLoader(),
            transformer = ChatTransformer()
        )
        MyViewerCustomizer().process(appActivity(), builder) // 添加自定义业务逻辑和UI处理

        if (fullScreen) {
            builder.setViewerFactory(object : ImageViewerDialogFragment.Factory() {
                override fun build() = FullScreenImageViewerDialogFragment()
            })
        }

        builder.show()
    }

    // 数据提供者 一次加载 or 分页
    private fun myDataProvider(clickedData: PhotoData): DataProvider {

        return if (loadAllAtOnce) {
            provideViewerDataProvider { listOf(clickedData) }
        } else {
            provideViewerDataProvider(
                loadInitial = { listOf(clickedData) },
                loadAfter = { id, callback -> Api.asyncQueryAfter(id, callback) },
                loadBefore = { id, callback -> Api.asyncQueryBefore(id, callback) }
            )
        }
    }

}